﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace davejk.Minecraft.ServerWrapper.Game
{
    public static class UserCommands
    {

        public static ServerCommandType GetCommandType(string cmd)
        {
            if (cmd.StartsWith("!give "))
                return ServerCommandType.GiveItem;

            if (cmd.StartsWith("!goto "))
                return ServerCommandType.Teleport;

            return ServerCommandType.NotACommand;
        }
        public enum ServerCommandType
        {
            GiveItem,
            Teleport,
            NotACommand
        }

        public static string[] Run(string player, string cmd, List<string> players)
        {
            //todo: time limitation on some commands

            if (cmd.StartsWith("!give"))
            {
                cmd = cmd.Substring("!give ".Length);

                string TellFormat = "tell {0} {1}";

                string target = string.Empty;
                if (cmd.StartsWith("me "))
                    target = player;
                else
                {
                    if (cmd.IndexOf(' ') > 1)
                        target = cmd.Substring(0, cmd.IndexOf(' '));
                    if (!players.Contains(target.ToLower()))
                        return new string[] { String.Format(TellFormat, player, "Give to who?") };
                }
                cmd = cmd.Substring(cmd.IndexOf(' ') + 1);

                int count = 0;
                bool SpecifiedNumber = false;
                if (cmd.IndexOf(' ') > 0)
                    SpecifiedNumber = Int32.TryParse(cmd.Substring(0, cmd.IndexOf(' ')), out count);

                if (SpecifiedNumber)
                    cmd = cmd.Substring(cmd.IndexOf(' ') + 1);

                MinecraftItem item = Items.GetItemByName(cmd);
                if (item == null)
                    return new string[] { String.Format(TellFormat, player, "That item doesn't exist or isn't spawnable.") };

                if (item.OpOnly && !Utils.Ops.IsOp(player))
                    return new string[] { String.Format(TellFormat, player, "Only ops can spawn " + item.NamePlural.ToLower() + "!") };
                if (target != player && !Utils.Ops.IsOp(player))
                    return new string[] { String.Format(TellFormat, player, "You can't give " + target + " items!") };

                if (SpecifiedNumber && count > item.Stack)
                    return new string[] { String.Format(TellFormat, player, "You can't spawn so many " + item.NamePlural.ToLower() + "!") };
                else if (!SpecifiedNumber)
                    count = item.Stack;

                return new string[] { 
                    String.Format(TellFormat, player, "OK! Giving " + (target == player ? "you" : target) + " " + count.ToString() + " " + (count == 1 ? item.Name.ToLower() : item.NamePlural.ToLower()) + "."),
                    "give " + (target == player ? player : target) + " " + item.Id + " " + count,
                    (target != player ? String.Format(TellFormat, target, player + " gave you " + count.ToString() + " " + (count == 1 ? item.Name.ToLower() : item.NamePlural.ToLower()) + ".") : string.Empty),
                };
            }
            else if (cmd.StartsWith("!goto "))
            {
                cmd = cmd.Substring("!goto ".Length);

                string TellFormat = "tell {0} {1}";

                string target = string.Empty;

                if (!players.Contains(cmd.ToLower()))
                    return new string[] { String.Format(TellFormat, player, String.Format("Can't find {0}!", cmd)) };

                if (cmd == player)
                    return new string[] { String.Format(TellFormat, player, "You're already there!") };

                return new string[] {
                    String.Format(TellFormat, cmd, String.Format("{0} teleported to you.", player)),
                    String.Format("tp {0} {1}", player, cmd)
                };
            }
            else
            {
                return new string[] { "say Unrecognised command!" };
            }
        }

    }
}
